class_name WeaponState_Common
extends WeaponState

@export var anim_name:String = ""
@export var additional_effects:Array[WeaponStateAnim_AdditionalEffect]
var reses:Array[Util.AddCallMethodTrackRes] = []
var is_anim_finished:bool = false
signal state_entered
signal state_exited

func on_anim_finished(anim_name:String):
	if self.anim_name == anim_name:
		is_anim_finished = true
func state_init(fsm:FSM):
	super.state_init(fsm)
func on_enter(from_state:State):
	super.on_enter(from_state)
	is_anim_finished = false
	var anim:Animation = state_manager.get_animation(anim_name)
	if anim:
		reses.clear()
		state_manager.add_anim_finished_event(anim_name,on_anim_finished,CONNECT_ONE_SHOT)
		var idx:int = 0
		for effect in additional_effects:
			if effect.time == 0:
				effect.init_additional_effect(self)
				reses.append_array(effect.excute())
			else:
				reses.append(Util.add_call_method_track_and_method(state_manager.weapon.animation_player,anim_name,effect.time,"on_additional_effect_excute",[idx],self))
			idx += 1
		state_manager.play_animation(anim_name)
	state_entered.emit()
func on_update(delta:float):
	super.on_update(delta)
func on_physics_update(delta:float):
	super.on_physics_update(delta)
func on_exit(to_state:State):
	state_exited.emit()
	reses.reverse()
	for res in reses:
		res.remove_track.call()
	reses.clear()
	super.on_exit(to_state)
func get_state_status() -> GameEnum.StateStatus:
	return super.get_state_status()
func on_additional_effect_excute(idx:int):
	if idx < additional_effects.size():
		var effect = additional_effects[idx]
		effect.init_additional_effect(self)
		reses.append_array(effect.excute())
		
